let login=document.getElementById('login')
let register=document.getElementById('register')
let form_box=document.getElementsByClassName('form-box')[0]

function showCustomAlert(message, duration) {
    let customAlert = document.getElementById('customAlert');
    if (!customAlert) {
        customAlert = document.createElement('div');
        customAlert.id = 'customAlert';

        const alertMessage = document.createElement('p');
        alertMessage.id = 'alertMessage';
        customAlert.appendChild(alertMessage);

        document.body.appendChild(customAlert);
    }

    const alertMessage = document.getElementById('alertMessage');

    alertMessage.textContent = message;
    customAlert.style.display = 'block';

    setTimeout(() => {
        customAlert.style.display = 'none';
    }, duration);
}
showCustomAlert(result, 3000);
register.addEventListener('click',()=>{
    form_box.style.transform='translateX(80%)'
})
login.addEventListener('click',()=>{
    form_box.style.transform='translateX(0%)'
})

function validateEmail() {
    const  email=document.querySelector(".input_txt").value;
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

    if (emailRegex.test(email)) {
        document.getElementById('resultEmail2').textContent = '';
    } else {
        document.getElementById('resultEmail2').textContent = '邮箱地址无效';
    }
}
const  input_email=document.querySelector(".input_txt")
input_email.addEventListener('input',validateEmail)

const input_code=document.getElementsByClassName('input_code')[0]
const loginButton=document.getElementsByClassName('button')[0]
function dosubmit(){

    if(input_email.value.trim()===''){
        input_email.value='请输入邮箱地址'
        input_email.style.color='red'
    }
    if(input_code.value.trim()===''){
        input_code.value='不能为空'
        input_code.style.color='red'

    }
}
input_email.addEventListener('focus',function(){
    if(this.value==='请输入邮箱地址'){
        this.value=''
        this.style.color='white'
    }
})

// const form=document.getElementsByClassName('login-box')[0]
// form.addEventListener('submit',function(event){
//     event.preventDefault()
// })
input_code.addEventListener('focus',function(){
    if(this.value==='不能为空'){
        this.value=''
        this.style.color='white'
    }
})
const getCodeButton = document.getElementById('get-code');


let countdownTime = 10;// 倒计时时间设置为60秒

let timer;
function sendCode(email,btn) {

    const filteredEmail=email.replace(/<[^>]*>/g, '')
    console.log(btn)


    if(filteredEmail){
        $.ajax({
            url:  '/login/loginCode/sendCode',
            type: 'POST',
            data: {email: email},
            success: function(result){
                showCustomAlert('获取验证码成功',3000)
                refresh(btn,60);
            },
            error:function(error){
                showCustomAlert('请求出错: ' + error.status + ' ' + error.statusText, 3000);
                console.log(error);
            }

        })
    }else{
        showCustomAlert('邮箱为空，请输入邮箱',3000)
    }

}


function refresh(btn,initialCountdown){
    let countdown=initialCountdown;
    let  timer;
    console.log(btn+"refresh")
    btn.disabled=true;
    btn.style.pointerEvents = 'none'
    /*  let time = localStorage.getItem("time"); */
    console.log(this)
    btn.textContent=`${countdown}s后重发`
    timer=setInterval(()=>{
        countdown--
        localStorage.setItem("time", countdown)
        if(countdown>0){
            btn.textContent=`${countdown}s后重发`
        }else{
            localStorage.clear();
            clearInterval(timer)
            btn.disabled=false
            // btn.classList.add('.getCode')
            btn.textContent=`获取验证码`
            btn.style.pointerEvents = 'auto'
        }
    },1000)
}
document.addEventListener('DOMContentLoaded', function () {
    let getCode = document.querySelector('#get-code');
    let btn = getCode;
    // 页面加载时检查是否有剩余时间
    let time = localStorage.getItem("time");
    if (time && parseInt(time) > 0) {
        refresh(btn, parseInt(time));
    }
})





















/*function validatePassword() {
    var password=document.querySelector(".input_password").value;
    const regex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{6,20}$/;
    console.log(regex)
    if (regex.test(password)) {
        console.log(regex)
        document.getElementById('resultPassword1').textContent = '';
    } else {
        document.getElementById('resultPassword1').textContent = '长度需在6到20位之间,且必须含有字母';
    }
}
var inputPassword=document.querySelector('.input_password')
inputPassword.addEventListener('input',validatePassword)


function validateEmail() {
    const  email=document.querySelector(".input_txt").value;
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
console.log(emailRegex)
    if (emailRegex.test(email)) {
        document.getElementById('resultEmail1').textContent = '';
    } else {
        document.getElementById('resultEmail1').textContent = '邮箱地址无效';
    }
}*/
